﻿using ML.Entity;
using ML.IBusiness;
using ML.IRepository;
using System.Collections.Generic;
using System.Linq;

namespace ML.Business
{
    /// <summary>
    /// 商户业务实现类
    /// </summary>
    public class MerchantBusiness : IMerchantBusiness
    {
        private readonly IMerchantRepository merchantRepository;
        private readonly IReceivablesRepository receivablesRepository;
        private readonly IBusinessActivitiesRepository businessActivitiesRepository;
        private readonly IProductCategoryRepository productCategoryRepository;
        private readonly ICapitalAccountRepository capitalAccountRepository;
        private readonly IOrderRepository orderRepository;
        public MerchantBusiness(IMerchantRepository _merchantRepository, IReceivablesRepository _receivablesRepository, IBusinessActivitiesRepository _businessActivitiesRepository,
            IProductCategoryRepository _productCategoryRepository, ICapitalAccountRepository _capitalAccountRepository, IOrderRepository _orderRepository)
        {
            merchantRepository = _merchantRepository;
            receivablesRepository = _receivablesRepository;
            businessActivitiesRepository = _businessActivitiesRepository;
            productCategoryRepository = _productCategoryRepository;
            capitalAccountRepository = _capitalAccountRepository;
            orderRepository = _orderRepository;
        }

        public BaseResult<Pager<MerchantResponse>> List(MerchantSearch merchantSearch)
        {
            //根据传递的条件查询
            Pager<MerchantResponse> data = merchantRepository.List(merchantSearch);
            return new BaseResult<Pager<MerchantResponse>>(200, data);
        }

        public BaseResult<MerchantResponse> GetDetailById(string id)
        {
            var merchantInfo = merchantRepository.GetDetailById(id);
            return new BaseResult<MerchantResponse>(200, merchantInfo);
        }

        public BaseResult<IEnumerable<ReceivablesEntity>> GetReceivablesByMerchantId(string id)
        {
            var data = receivablesRepository.GetReceivablesByMerchantId(id);
            return new BaseResult<IEnumerable<ReceivablesEntity>>(200, data);
        }

        public BaseResult<IEnumerable<BusinessActivitiesEntity>> GetBusinessActivitiesByMerchantId(string id)
        {
            var data = businessActivitiesRepository.GetBusinessActivitiesByMerchantId(id);
            return new BaseResult<IEnumerable<BusinessActivitiesEntity>>(200, data);
        }

        public BaseResult<IEnumerable<ProductCategoryEntity>> GetProductCategoryByMerchantId(string id)
        {
            var data = productCategoryRepository.GetProductCategoryByMerchantId(id);
            return new BaseResult<IEnumerable<ProductCategoryEntity>>(200, data);
        }

        public BaseResult<IEnumerable<MerchantEntity>> GetListByPlatFormId(string id, int pageindex, int pagesize)
        {
            var data = merchantRepository.GetListByPlatFormId(id, pageindex, pagesize);
            if (data.Count() <= 0)
            {
                return new BaseResult<IEnumerable<MerchantEntity>>(202);
            }
            return new BaseResult<IEnumerable<MerchantEntity>>(200, data);
        }

        public BaseResult<IEnumerable<MerchantEntity>> GetNoDelBusinessList(string name, int pageindex, int pagesize)
        {
            var data = merchantRepository.GetNoDelBusinessList(name, pageindex, pagesize);
            if (data.Count() <= 0)
            {
                return new BaseResult<IEnumerable<MerchantEntity>>(202);
            }
            return new BaseResult<IEnumerable<MerchantEntity>>(200, data);
        }

        public BaseResult<IEnumerable<MerchantEntity>> GetNoDelBusinessListByRating(string name, int pageindex, int pagesize, int ratingType, bool isDesc)
        {
            var data = merchantRepository.GetNoDelBusinessListByRating(name, pageindex, pagesize,ratingType,isDesc);
            if (data.Count() <= 0)
            {
                return new BaseResult<IEnumerable<MerchantEntity>>(202);
            }
            return new BaseResult<IEnumerable<MerchantEntity>>(200, data);
        }

        public BaseResult<MerchantEntity> GetInfoById(string id)
        {
            var data = merchantRepository.GetInfoById(id);
            if (data == null)
            {
                return new BaseResult<MerchantEntity>(202);
            }
            return new BaseResult<MerchantEntity>(200, data);
        }

        public BaseResult<MerchantEntity> WXgetInfoByUserId(string loginCode)
        {
            var data = merchantRepository.GetInfoByUserId(loginCode);
            if (data == null)
            {
                return new BaseResult<MerchantEntity>(202);
            }
            return new BaseResult<MerchantEntity>(200, data);
        }

        public BaseResult<IEnumerable<MerchantProductListEntity>> GetProductCategoryAndInfoById(string id)
        {
            //查询该商户所有产品分类信息
            var productCategoryEntitys = productCategoryRepository.GetNoDelProductCategoryByMerchantId(id);
            if (productCategoryEntitys.Count() <= 0)
            {
                return new BaseResult<IEnumerable<MerchantProductListEntity>>(202);
            }

            //将产品分类Id组合成数组
            List<string> productcategory_ids = new List<string>();
            foreach (var productCategoryEntity in productCategoryEntitys)
            {
                productcategory_ids.Add(productCategoryEntity.Id);
            }

            //根据产品分类Id查询产品信息
            var productEntitys = merchantRepository.GetProductCategoryAndInfoById(productcategory_ids);
            if (productEntitys.Count() <= 0)
            {
                return new BaseResult<IEnumerable<MerchantProductListEntity>>(202);
            }

            //处理数据信息
            List<MerchantProductListEntity> data = new List<MerchantProductListEntity>();
            var i = 1;
            foreach (var productCategoryEntity in productCategoryEntitys)
            {
                MerchantProductListEntity merchantProduct = new MerchantProductListEntity();
                merchantProduct.Id = productCategoryEntity.Id;
                merchantProduct.Name = productCategoryEntity.Name;
                merchantProduct.Sort = productCategoryEntity.Sort;
                merchantProduct.type_accumulation = "c" + i++;
                merchantProduct.list = returnProductEntity(productCategoryEntity.Id, productEntitys);
                data.Add(merchantProduct);
            }
            return new BaseResult<IEnumerable<MerchantProductListEntity>>(200, data);
        }

        public BaseResult<MerchantEntity> GetNoDelInfoByUserId(string id)
        {
            var data = merchantRepository.GetInfoByUserId(id);
            if (data == null)
            {
                return new BaseResult<MerchantEntity>(202);
            }
            return new BaseResult<MerchantEntity>(200, data);
        }

        public BaseResult<bool> Add(MerchantEntity merchantEntity)
        {
            //根据用户Id查询商户是否存在店铺，如果已经存在则不允许插入，否则直接插入
            var data = merchantRepository.GetInfoByUserId(merchantEntity.UserId);
            if (data != null)
            {
                return new BaseResult<bool>(1053);
            }

            // 组装商户资金账户表实体信息
            var capitalEntity = new CapitalAccountEntity()
            {
                MerchantId = merchantEntity.Id,
                Balance = 0,
                Withdrawed = 0,
            };
            bool isTrue = merchantRepository.Add(merchantEntity, capitalEntity);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201);
        }

        public BaseResult<bool> Update(MerchantEntity merchantEntity)
        {
            var isTrue = merchantRepository.Update(merchantEntity);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> UpdateBusinessState(string Id, string JoinMark)
        {
            var data = merchantRepository.UpdateBusinessState(Id, JoinMark);
            return new BaseResult<bool>(200, data);
        }

        public BaseResult<bool> Delete(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                return new BaseResult<bool>(1001, false);
            }

            //判断商户是否含有订单，如果含有订单则不允许删除
            var data = orderRepository.GetOrderInfoByMerchantId(id);
            if (data.Count() > 0)
            {
                return new BaseResult<bool>(1051, false);
            }

            var isTrue = merchantRepository.Delete(id);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<IEnumerable<MerchantEntity>> GetAll()
        {
            var data = merchantRepository.GetAll();
            if (data.Count() <= 0)
            {
                return new BaseResult<IEnumerable<MerchantEntity>>(202);
            }
            return new BaseResult<IEnumerable<MerchantEntity>>(200, data);
        }

        private List<ProductEntity> returnProductEntity(string productCategoryId, IEnumerable<ProductEntity> productEntitys)
        {
            List<ProductEntity> list = new List<ProductEntity>();
            foreach (var productEntity in productEntitys)
            {
                if (productEntity.CategoryId == productCategoryId)
                {
                    list.Add(productEntity);
                }
            }
            return list;
        }
    }
}